// pages/buy/buy.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selected: 0,
        orderList:[],
        orderList1:[],
        orderList2:[],
        imageList:[],
        list: ['全部订单','未发货','待收货'],
    },
    //tab框
    selected: function (e) {
        let that= this
        let index = e.currentTarget.dataset.index
        if( index == 0){  
        that.setData({
            selected: 0
        })
        }else if( index == 1) {
        that.setData({
            selected: 1
        })
        }else{
        that.setData({
            selected: 2
        })
        }
        this.onLoad()
    },
    //获取订单信息
    getOrderList(){
        wx.get('http://localhost:8000/wx/getOrderList?id='+wx.getStorageSync('userInfo')._id).then(res => {
            if(res.code == 200){
                res.data.forEach(a => {
                    if (a.createTime){
                      let time = a.createTime
                      let date = new Date(time)
                      let times=date.getFullYear() + '-' + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1) )+ '-' + (date.getDate()<10?"0"+date.getDate():date.getDate() ) +' '+ (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' +(date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
                      a.createTime = times
                    }
                    if(a.isGetType === 0) {
                        let noSend = []
                        noSend.push(a)
                        this.setData({
                            orderList1:noSend
                        })
                    } else if (a.isGetType === 1) {
                        let noGetGoods = []
                        noGetGoods.push(a)
                        this.setData({
                            orderList2:noGetGoods
                        })
                    }
                  })
                this.setData({
                    orderList:res.data,
                })
            }
        })
    },
    //删除订单
    delOrder(e){
        var that = this
        wx.showModal({
          title: '删除提醒',
          content:'是否删除订单？',
          success(res){
              if(res.confirm){
                wx.delete('http://localhost:8000/wx/deleteOrder',{orderId:e.currentTarget.dataset.id}).then(res => {
                    if(res.code == 200){
                        that.onLoad()
                    }
                })
              }
          }
        })
    },
    //确认收货
    getDelivery(e){
        var that = this
        wx.showModal({
          title: '收获提醒',
          content:"是否确认收货",
          success(res){
              if(res.confirm){
                wx.post('http://localhost:8000/wx/updateOrderState',{orderId:e.currentTarget.dataset.id,isGetType:2}).then(res => {
                    if(res.code == 200){
                        that.onLoad()
                    }
                })
              }
          }
        })
    },
    //点击跳转购买详情
    goodsDetail(e){
        wx.navigateTo({
            url: '/pages/orderDetail/orderDetail?item=' + JSON.stringify(e.currentTarget.dataset.shopping)
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getOrderList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})